package com.shanghaionstar.sqlite;

import android.content.Context;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.sqlite.entities.UserAddressEntity;
import com.onstar.cn.sqlite.xutil.DbUtils;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.exception.DbException;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.utils.ApplicationUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jun chen
 * @ClassName: UserAddressInfoImpl
 * @date 2016年4月11日 上午10:39:58
 * @Description: TODO
 */
public class UserAddressInfoImpl {

    private Context context;
    private DbUtils utils;

    //	private static final int DBVERSION = 1;
    public UserAddressInfoImpl(Context context) {
        this.context = context;
        utils = OnstarApplication.getmDbUtils();
    }

    /**
     * @param @param poiItemInfo
     * @param @param customCatetory 1：家  ，2 公司
     * @return void
     * @Title: saveUserAddressInfo
     * @Description: 保存用户家和公司的信息
     */
    public void saveUserAddressInfo(PoiItemInfo poiItemInfo, String customCatetory) {
        UserAddressEntity userAddressEntity = null;
        try {
            userAddressEntity = utils.findFirst(Selector.from(UserAddressEntity.class)
                    .where("customerCatetory", "=", customCatetory).and("userId", "=", ApplicationUtil.getInstance().getIdpID()));
            if (userAddressEntity == null) {
                userAddressEntity = new UserAddressEntity();
            }
            userAddressEntity.setTitle(poiItemInfo.getTitle());
            userAddressEntity.setSnippet(poiItemInfo.getAddress());
            userAddressEntity.setLatitude(poiItemInfo.getLatitude());
            userAddressEntity.setLongitude(poiItemInfo.getLongitude());
            userAddressEntity.setCustomerCatetory(customCatetory);
            userAddressEntity.setPoiCityCode(poiItemInfo.getCityCode());
            userAddressEntity.setPoiPhoneNumber(poiItemInfo.getTelphone());
            if (!CommonUtil.isNull(ApplicationUtil.getInstance().getIdpID())) {
                userAddressEntity.setUserId(ApplicationUtil.getInstance().getIdpID());
            }
            utils.saveOrUpdate(userAddressEntity);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param @param  customerCatetory 1：家， 2 公司
     * @param @return
     * @return PoiItemInfo
     * @Title: quaryUserAddressInfo
     * @Description: 查询用户的家或是公司的地址信息
     */
    public PoiItemInfo quaryUserAddressInfo(String customerCatetory) {
        UserAddressEntity userAddressEntity = null;
        PoiItemInfo poiItemInfo = null;
        List<UserAddressEntity> list = null;
        try {
            list = utils.findAll(Selector.from(UserAddressEntity.class)
                    .where("customerCatetory", "=", customerCatetory).and("userId", "=", ApplicationUtil.getInstance().getIdpID()));
            if (list != null && list.size() != 0) {
                userAddressEntity = list.get(0);
                poiItemInfo = new PoiItemInfo();
                poiItemInfo.setTitle(userAddressEntity.getTitle());
                poiItemInfo.setAddress(userAddressEntity.getSnippet());
                poiItemInfo.setLatitude(userAddressEntity.getLatitude());
                poiItemInfo.setLongitude(userAddressEntity.getLongitude());
                poiItemInfo.setCityCode(userAddressEntity.getPoiCityCode());
                poiItemInfo.setTelphone(userAddressEntity.getPoiPhoneNumber());
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return poiItemInfo;
    }


   public List<UserAddressEntity> getAll(){
       try {
          return utils.findAll(UserAddressEntity.class);
       } catch (DbException e) {
           e.printStackTrace();
       }
       return  new ArrayList<>();
   }

    /**
     * @Title: DeleterTableInfo
     * @Description: 清除表中的数据
     */
    public void deleteTableInfo()  {
//        UserAddressEntity userAddressEntity = new UserAddressEntity();
//        String tableName = userAddressEntity.getTableName();
        try {
            utils.dropTable(UserAddressEntity.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


}
